import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_date_type.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_file_upload_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_high_input_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_sure_sheet.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/ext.dart';

import 'disposal_details_controller.dart';

/// 处置详情
class DisposalDetailsPage extends StatelessWidget {
  DisposalDetailsPage({super.key});

  final controller = Get.put(DisposalDetailsController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          backgroundColor: const Color(0xFFF7FBFE),
          appBar: CustomAppBar(
            leading: IconButton(icon: const Icon(Icons.arrow_back_ios_new), iconSize: 24.w, color: const Color(0xFFFFFFFF), onPressed: Get.back),
            title: Text(
              '详情',
              style: TextStyle(
                fontWeight: FontWeight.w400,
                color: const Color(0xFFFFFFFF),
                fontSize: 16.sp,
              ),
            ),
            backgroundColor: const Color(0xFF0462DC),
          ),
          body: Stack(
            children: [
              Container(
                alignment: Alignment.center,
                height: 102.w,
                color: const Color(0xFF0462DC),
              ),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      AppGaps.vGap16,
                      CustomTitleChartPage(
                        contentPadding: EdgeInsets.only(right: 16.w, left: 16.w, top: 16.w),
                        title: controller.entity.companyName,
                        titleRightWidget: controller.listController.getStatueView(controller.entity.status),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Expanded(child: _getTextView(title: '点位:', value: controller.entity.deviceCode)),
                                Expanded(child: _getTextView(title: '报警级别:', value: CustomDateType.getAlarmType(controller.entity.alarmLevel,controller.entity.dataType),),)
                              ],
                            ),
                            Row(
                              children: [
                                Expanded(child: _getTextView(title: '系统-类别:', value:  '${controller.entity.systemName}-${controller.entity.monitorName}',),),
                                Expanded(child: _getTextView(title: '报警数值:', value: '${controller.entity.val}')),
                              ],
                            ),
                            Row(
                              children: [
                                Expanded(child: _getTextView(title: '系统名称:', value: controller.entity.systemName)),
                                Expanded(child: _getTextView(title: '监测类型:', value: controller.entity.monitorName)),
                              ],
                            ),
                            _getTextView(title: '报警类型:', value: CustomDateType.getAlarmTypeByValue(controller.entity.dataType)),
                            _getTextView(title: '报警开始:', value:controller.entity.alarmStartTime!=0? Jiffy.parseFromMillisecondsSinceEpoch(controller.entity.alarmStartTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
                            _getTextView(title: '报警结束:', value: controller.entity.alarmEndTime!=0?Jiffy.parseFromMillisecondsSinceEpoch(controller.entity.alarmEndTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'):DateTime.now().toStr),
                            // _getTextView(title: '报警结束:', value: DateTime.now().toStr),
                            AppGaps.vGap6,
                          ],
                        ),
                      ),
                      AppGaps.vGap16,
                      //处置信息
                      _getDisposalInfo(),
                      AppGaps.vGap16,
                      //消警信息
                      _getFireAlarmInfo(),
                      AppGaps.vGap60,
                    ],
                  ),
                ),
              ),
            ],
          ),
          bottomSheet: controller.type != 3 ? CustomSureSheet(onTap: controller.confirm) : null,
        );
      },
    );
  }

  /// 处置信息
  Widget _getDisposalInfo() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: Column(
        children: [
          CustomHighInputPage(
            title: '处置措施:',
            isEnabled: controller.type == 1,
            isRequired: false,
            isShowDivider: false,
            text: controller.entity.disposalMeasures,
            hintText: '请输入处置措施',
            onCallBack: (value) {
              controller.entity.disposalMeasures = value;
            },
          ),
          Divider(height: 0.5.w, color: const Color(0xffEAF2FA)).marginSymmetric(vertical: 20.w),
          CustomFileUploadPage(
            isEnabled: controller.type == 1,
            isRequired: false,
            isShowDivider: false,
            splitCommaInUrl: true,
            uploadedFiles: controller.disposalMeasuresAnnex,
            title: '附件',
            onCallBack: (value) {
              controller.disposalMeasuresAnnex = value;
            },
          ),
          AppGaps.vGap16
        ],
      ),
    );
  }

  /// 消警信息
  Widget _getFireAlarmInfo() {
    return Visibility(
      visible: controller.type != 1,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.w),
        ),
        child: Column(
          children: [
            CustomHighInputPage(
              title: '消警描述:',
              isEnabled: controller.type == 2,
              isRequired: false,
              isShowDivider: false,
              text: controller.entity.disarmingDescription,
              hintText: '请输入消警描述',
              onCallBack: (value) {
                controller.entity.disarmingDescription = value;
              },
            ),
            Divider(height: 0.5.w, color: const Color(0xffEAF2FA)).marginSymmetric(vertical: 20.w),
            CustomFileUploadPage(
              isEnabled: controller.type == 2,
              isRequired: false,
              isShowDivider: false,
              splitCommaInUrl: true,
              uploadedFiles: controller.disarmingDescriptionAnnex,
              title: '附件',
              onCallBack: (value) {
                controller.disarmingDescriptionAnnex = value;
              },
            ),
            AppGaps.vGap16
          ],
        ),
      ),
    );
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xffC7CEE5),
              ),
            ),
            AppGaps.hGap6,
            Expanded(
                child: Text(
              value,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xff1D3452),
              ),
            ))
          ],
        ),
        AppGaps.vGap10,
      ],
    );
  }


}
